package com.rickpan.demo;

import com.rickpan.storage.StorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 存储切换演示工具
 * 在应用启动时展示当前使用的存储类型和相关配置
 */
@Component
@ConditionalOnProperty(name = "app.demo.storage-switch", havingValue = "true", matchIfMissing = false)
public class StorageSwitchDemo implements CommandLineRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(StorageSwitchDemo.class);
    
    @Autowired
    private ApplicationContext applicationContext;
    
    @Autowired
    private StorageService storageService;
    
    @Override
    public void run(String... args) throws Exception {
        logger.info("=".repeat(60));
        logger.info("🔄 存储类型切换演示");
        logger.info("=".repeat(60));
        
        // 显示当前存储类型
        showCurrentStorageType();
        
        // 显示加载的存储服务Bean
        showLoadedStorageBeans();
        
        // 显示存储服务功能
        showStorageServiceCapabilities();
        
        // 显示配置信息
        showConfigurationInfo();
        
        logger.info("=".repeat(60));
        logger.info("✅ 存储切换演示完成");
        logger.info("=".repeat(60));
    }
    
    /**
     * 显示当前存储类型
     */
    private void showCurrentStorageType() {
        String storageType = storageService.getStorageType();
        String serviceClass = storageService.getClass().getSimpleName();
        
        logger.info("📦 当前存储类型: {}", storageType.toUpperCase());
        logger.info("🔧 存储服务实现: {}", serviceClass);
        
        switch (storageType) {
            case "local":
                logger.info("💾 使用本地文件系统存储");
                break;
            case "minio":
                logger.info("☁️ 使用 MinIO 对象存储");
                break;
            default:
                logger.info("❓ 未知存储类型: {}", storageType);
        }
    }
    
    /**
     * 显示加载的存储服务Bean
     */
    private void showLoadedStorageBeans() {
        logger.info("📋 已加载的存储相关Bean:");
        
        // 检查本地存储服务
        if (applicationContext.containsBean("localStorageServiceImpl")) {
            logger.info("  ✅ LocalStorageServiceImpl - 本地存储服务");
        } else {
            logger.info("  ❌ LocalStorageServiceImpl - 未加载");
        }
        
        // 检查 MinIO 存储服务
        if (applicationContext.containsBean("minIOStorageServiceImpl")) {
            logger.info("  ✅ MinIOStorageServiceImpl - MinIO存储服务");
        } else {
            logger.info("  ❌ MinIOStorageServiceImpl - 未加载");
        }
        
        // 检查 MinIO 客户端
        if (applicationContext.containsBean("minioClient")) {
            logger.info("  ✅ MinioClient - MinIO客户端");
        } else {
            logger.info("  ❌ MinioClient - 未加载");
        }
        
        // 检查 MinIO 配置
        if (applicationContext.containsBean("minIOProperties")) {
            logger.info("  ✅ MinIOProperties - MinIO配置属性");
        } else {
            logger.info("  ❌ MinIOProperties - 未加载");
        }
        
        // 检查健康检查器
        if (applicationContext.containsBean("minIOHealthIndicator")) {
            logger.info("  ✅ MinIOHealthIndicator - MinIO健康检查");
        } else {
            logger.info("  ❌ MinIOHealthIndicator - 未加载");
        }
        
        // 检查监控组件
        if (applicationContext.containsBean("minIOMetrics")) {
            logger.info("  ✅ MinIOMetrics - MinIO监控指标");
        } else {
            logger.info("  ❌ MinIOMetrics - 未加载");
        }
    }
    
    /**
     * 显示存储服务功能
     */
    private void showStorageServiceCapabilities() {
        logger.info("🛠️ 存储服务功能:");
        logger.info("  📤 文件上传: 支持");
        logger.info("  📥 文件下载: 支持");
        logger.info("  🗂️ 分片上传: 支持");
        logger.info("  📋 文件复制: 支持");
        logger.info("  🗑️ 文件删除: 支持");
        logger.info("  🔍 文件检查: 支持");
        logger.info("  🔗 URL生成: 支持");
        logger.info("  🔐 哈希计算: 支持");
        
        // 初始化存储
        try {
            storageService.initStorage();
            logger.info("  ✅ 存储初始化: 成功");
        } catch (Exception e) {
            logger.error("  ❌ 存储初始化: 失败 - {}", e.getMessage());
        }
    }
    
    /**
     * 显示配置信息
     */
    private void showConfigurationInfo() {
        logger.info("⚙️ 配置信息:");
        
        // 获取存储类型配置
        String storageType = applicationContext.getEnvironment().getProperty("app.storage.type", "local");
        logger.info("  存储类型: {}", storageType);
        
        if ("local".equals(storageType)) {
            String storagePath = applicationContext.getEnvironment().getProperty("app.storage.path", "./uploads");
            String maxSize = applicationContext.getEnvironment().getProperty("app.storage.max-size", "104857600");
            
            logger.info("  本地存储路径: {}", storagePath);
            logger.info("  最大文件大小: {} 字节", maxSize);
            
        } else if ("minio".equals(storageType)) {
            String endpoint = applicationContext.getEnvironment().getProperty("app.storage.minio.endpoint", "http://localhost:9000");
            String bucketName = applicationContext.getEnvironment().getProperty("app.storage.minio.bucket-name", "rickpan");
            String region = applicationContext.getEnvironment().getProperty("app.storage.minio.region", "us-east-1");
            String urlExpiration = applicationContext.getEnvironment().getProperty("app.storage.minio.default-url-expiration", "3600");
            
            logger.info("  MinIO端点: {}", endpoint);
            logger.info("  存储桶名称: {}", bucketName);
            logger.info("  区域: {}", region);
            logger.info("  URL有效期: {} 秒", urlExpiration);
        }
    }
}
